//
//  TogetherStopSettingView.m
//  Together_iOS
//
//  Created by applex on 14-3-1.
//  Copyright (c) 2014年 QIU Shuang. All rights reserved.
//

#import "TogetherStopSettingView.h"
#import "TogetherCellButton.h"
#import "UIButton+Additions.h"
#import "UIImage+Additions.h"
#import "NSDate+Helper.h"

#import "TogetherDatePickerController.h"
#import "TogetherLocationSelectorViewController.h"
#import "TogetherRegionSelectorViewController.h"
#import "Plan.h"

#import "TogetherStoreListViewController.h"

#import "AFNetworking.h"
#import "TogetherHttpClient.h"

@interface TogetherStopSettingView () <TogetherDatePickerControllerDelegate>

@property (nonatomic, strong) UIButton *eatingButton;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *movieButton;
@property (nonatomic, strong) UIView *seperatLineView1;
@property (nonatomic, strong) UIView *seperatLineView2;
@property (nonatomic, strong) UIView *seperatLineView3;
@property (nonatomic, strong) TogetherCellButton *locationSelectionButton;
@property (nonatomic, strong) TogetherCellButton *timeSelectionButton;
@property (nonatomic, strong) UIButton *doneButton;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIImageView *topBorderView;
@property (nonatomic, strong) NSArray *typeButtonSelectedBackgrounds;

@property (nonatomic, assign) NSInteger selectedType;
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, strong) NSString *selectedLocation;

@end

@implementation TogetherStopSettingView


- (void)setStop:(Stop *)stop {
    _stop = stop;
    if (!stop) {
        self.selectedLocation = @"";
        self.selectedType = 1;
        self.selectedDate = [NSDate dateFromString:@"1970-01-01 12:00" withFormat:@"yyyy-MM-dd HH:mm"];
    } else {
        self.selectedLocation = stop.regions;
        self.selectedType = [stop.type intValue];
        self.selectedDate = stop.startTime;
    }
}


- (void)setSelectedDate:(NSDate *)selectedDate {
    _selectedDate = selectedDate;
    [self.timeSelectionButton setDesString:[selectedDate stringWithFormat:@"hh:mm"]];
}


- (void)setSelectedLocation:(NSString *)selectedLocation {
    _selectedLocation = selectedLocation;
    if (selectedLocation.length) {
        [self.locationSelectionButton setDesString:selectedLocation];
    } else {
        [self.locationSelectionButton setDesString:@"选择位置"];
    }
}


- (void)setSelectedType:(NSInteger)selectedType {
    _selectedType = selectedType;
    
    for (UIView *view in self.typeButtonSelectedBackgrounds) {
        view.hidden = YES;
    }
    
    UIView *selectedView = [self.typeButtonSelectedBackgrounds objectAtIndex:selectedType - 1];
    selectedView.hidden = NO;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _eatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _eatingButton.frame = CGRectMake(10.0f, 24.0f, 78.0, 100.0f);
        [_eatingButton setTitle:@"餐饮" forState:UIControlStateNormal];
        [_eatingButton setTitleColor:[UIColor colorWithWhite:147./255. alpha:1.0f] forState:UIControlStateNormal];
        [_eatingButton setImage:[UIImage imageNamed:@"icon_canyin"] forState:UIControlStateNormal];
        [_eatingButton placeImageAboveText:2.0f];
        [_eatingButton addTarget:self action:@selector(typeButtonClicked:) forControlEvents:UIControlEventTouchDown];
        _eatingButton.tag = EVENT_FOOD;
        [self addSubview:_eatingButton];
        
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _playButton.frame = CGRectMake(100.0f, 24.0f, 78.0, 100.0f);
        [_playButton setTitle:@"娱乐" forState:UIControlStateNormal];
        [_playButton setTitleColor:[UIColor colorWithWhite:147./255. alpha:1.0f] forState:UIControlStateNormal];
        [_playButton setImage:[UIImage imageNamed:@"icon_yule"] forState:UIControlStateNormal];
        [_playButton placeImageAboveText:2.0f];
        [_playButton addTarget:self action:@selector(typeButtonClicked:) forControlEvents:UIControlEventTouchDown];
        _playButton.tag = EVENT_ENTERTAINMENT;
        [self addSubview:_playButton];
        
        _movieButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _movieButton.frame = CGRectMake(200., 24.0f, 78.0, 100.0f);
        [_movieButton setTitle:@"电影" forState:UIControlStateNormal];
        [_movieButton setTitleColor:[UIColor colorWithWhite:147./255. alpha:1.0f] forState:UIControlStateNormal];
        [_movieButton setImage:[UIImage imageNamed:@"icon_dianyin"] forState:UIControlStateNormal];
        [_movieButton placeImageAboveText:2.0f];
        [_movieButton addTarget:self action:@selector(typeButtonClicked:) forControlEvents:UIControlEventTouchDown];
        _movieButton.tag = EVENT_MOVIES;
        [self addSubview:_movieButton];
        
        _seperatLineView1 = [[UIView alloc] initWithFrame:CGRectMake(35., 153., 227., 1.)];
        _seperatLineView1.backgroundColor = [UIColor colorWithWhite:160./225. alpha:1.0];
        [self addSubview:_seperatLineView1];
        
        _locationSelectionButton = [[TogetherCellButton alloc] initWithFrame:CGRectMake(35., 154., 227., 45.)];
        [_locationSelectionButton setDesString:@"选择位置"];
        self.selectedLocation = @"";
        [_locationSelectionButton setIconImage:[UIImage imageNamed:@"icon_place"]];
        [_locationSelectionButton addTarget:self action:@selector(showLocationSelector:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_locationSelectionButton];
        
        _seperatLineView2 = [[UIView alloc] initWithFrame:CGRectMake(35., 153. + 45., 227., 1.)];
        _seperatLineView2.backgroundColor = [UIColor colorWithWhite:160./225. alpha:1.0];
        [self addSubview:_seperatLineView2];
        
        _timeSelectionButton = [[TogetherCellButton alloc] initWithFrame:CGRectMake(35., 200., 227., 45.)];
        self.selectedDate = [NSDate dateFromString:@"1970-01-01 12:00" withFormat:@"yyyy-MM-dd HH:mm"];
        [_timeSelectionButton setIconImage:[UIImage imageNamed:@"icon_clock"]];
        [_timeSelectionButton addTarget:self action:@selector(showTimeSelector:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_timeSelectionButton];
        
        _seperatLineView3 = [[UIView alloc] initWithFrame:CGRectMake(35., 153. + 45. * 2, 227., 1.)];
        _seperatLineView3.backgroundColor = [UIColor colorWithWhite:160./225. alpha:1.0];
        [self addSubview:_seperatLineView3];

        _doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _doneButton.frame = CGRectMake(15., 273., 262, 40.);
        [_doneButton setBackgroundColor:[UIColor colorWithRed:239./255. green:44./255. blue:38./255. alpha:1.0]];
//        [_doneButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:239./255. green:44./255. blue:38./255. alpha:1.0]] forState:UIControlStateNormal];
        _doneButton.layer.cornerRadius = 4.0f;
        [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
        [_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_doneButton];
        
        _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _cancelButton.frame = CGRectMake(15., 273. + 15. + 40., 262, 40.);
        _cancelButton.layer.cornerRadius = 4.0f;
        [_cancelButton setBackgroundColor:[UIColor colorWithWhite:150./255. alpha:1.0]];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
        
        _topBorderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_yuanjiao"]];
        _topBorderView.frame = CGRectMake(0.0f, -6.0f, 294., 6.0f);
        [self addSubview:_topBorderView];
        self.clipsToBounds = NO;
        
        UIView *backgroundView1 = [[UIView alloc] initWithFrame:CGRectMake(4, 13., 91, 116)];
        UIView *backgroundView2 = [[UIView alloc] initWithFrame:CGRectMake(94., 13., 91, 116)];
        UIView *backgroundView3 = [[UIView alloc] initWithFrame:CGRectMake(194., 13., 91, 116)];
        
        _typeButtonSelectedBackgrounds = @[backgroundView1, backgroundView2, backgroundView3];

        for (UIView *view in _typeButtonSelectedBackgrounds) {
            view.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
            view.hidden = YES;
            view.layer.cornerRadius = 6.0f;
            [self addSubview:view];
            [self sendSubviewToBack:view];
        }
        
        self.selectedType = EVENT_FOOD;
    }
    
    return self;
}


- (void)showSettingView:(id)sender {
    CGRect originFrame = CGRectMake(13., self.superview.frame.size.height, 294., 389.);
    CGRect afterFrame = originFrame;
    afterFrame.origin.y = self.superview.frame.size.height - 389;
    self.alpha = 0;
    self.frame = originFrame;
    
    [UIView animateWithDuration:0.25f animations:^{
        self.alpha = 1;
        self.frame = afterFrame;
    }];
    [self updateRecommendedLocation];
}


- (void)hideSettingView:(id)sender {
    CGRect originFrame = CGRectMake(13., self.superview.frame.size.height, 294., 389.);
    CGRect afterFrame = originFrame;
    afterFrame.origin.y = self.superview.frame.size.height - 389.;
    self.alpha = 1;
    self.frame = originFrame;
    
    [UIView animateWithDuration:0.25f animations:^{
        self.alpha = 0;
        self.frame = originFrame;
    }];
}


#pragma mark - Actions

- (void)updateRecommendedLocation {
    NSString *url = [[TogetherHttpClient serverAddress] stringByAppendingString:@"/business/recommendRegion?userIds=1,2"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSString *region = [responseObject objectForKey:@"region"];
        [self.locationSelectionButton setDesString:region];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
}


- (void)cancel:(id)sender {
    [self hideSettingView:nil];
}


- (void)done:(id)sender {
    [TogetherHttpClient fetchPredictedStopsWithRegion:self.selectedLocation type:self.selectedType before:nil after:nil excludesIds:nil userId:@(1) success:^(NSArray *stops) {
        [self.delegate recommendationReceived:stops onDate:self.selectedDate];
    } failure:^(NSError *error) {
        NSLog(@"Operation failed %@", error);
    }];
    [self hideSettingView:nil];
}


- (void)typeButtonClicked:(UIButton *)button {
    self.selectedType = button.tag;
}


- (void)showLocationSelector:(id)sender {
    TogetherRegionSelectorViewController *locationSelectorViewController = [[TogetherRegionSelectorViewController alloc] init];
    locationSelectorViewController.settingView = self;
    [self.viewController.navigationController pushViewController:locationSelectorViewController animated:YES];
}


- (void)showTimeSelector:(id)sender {
    TogetherDatePickerController *datePickerController = [[TogetherDatePickerController alloc] init];
    datePickerController.delegate = self;
    datePickerController.timeInterval = 30;
    
    if (self.viewController) {
        self.viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
        [self.viewController.navigationController pushViewController:datePickerController animated:YES];
    } else {
        [self cancel:nil];
    }
}


#pragma mark - TogetherDatePickerControllerDelegate

- (void)datePickerController:(TogetherDatePickerController *)pickerController didSelectDate:(NSDate *)date {
    self.selectedDate = date;
}


- (void)didSelectRegion:(NSString *)regionString {
    self.selectedLocation = regionString;
}


@end
